<html>
<head><meta charset="utf-8"><title>Cast to trait object · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Cast.20to.20trait.20object.html">Cast to trait object</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="230736898"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Cast%20to%20trait%20object/near/230736898" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mark R. Tuttle <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Cast.20to.20trait.20object.html#230736898">(Mar 17 2021 at 17:51)</a>:</h4>
<p>Could someone explain how the cast below is implemented?</p>
<div class="codehilite"><pre><span></span><code>let s: Arc&lt;Mutex&lt;dyn Subscriber&gt; = Arc::new(Mutex::new(DummySubscriber::new()));
</code></pre></div>
<p>Here DummySubscriber is a struct type that implements the trait Subscriber.</p>
<p>The thing is that the value DummySubscriber that needs to be cast to a Subscriber is sitting on the heap behind pointers implementing Arc.  Does the cast allocate a new value on the heap to hold the DummySubscriber and the vtable for the object DummySubscriber cast as trait object Subscriber?</p>



<a name="230737508"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Cast%20to%20trait%20object/near/230737508" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Cast.20to.20trait.20object.html#230737508">(Mar 17 2021 at 17:55)</a>:</h4>
<p>I think the cast here happens after <code>Mutex::new</code> but before <code>Arc::new</code>.</p>



<a name="230738179"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Cast%20to%20trait%20object/near/230738179" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mark R. Tuttle <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Cast.20to.20trait.20object.html#230738179">(Mar 17 2021 at 17:58)</a>:</h4>
<p>I don't think so.  If I look at the mir with <code>rustc --emit mir</code>, I see</p>
<div class="codehilite"><pre><span></span><code>_4 = move _5 as std::sync::Arc&lt;std::sync::Mutex&lt;dyn Subscriber&gt;&gt; (Pointer(Unsize));
</code></pre></div>
<p>The value _5 is constructed as I would expect from the rhs of the assignment, and the cast happens in this line on the already-constructed object.</p>



<a name="230739109"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Cast%20to%20trait%20object/near/230739109" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Cast.20to.20trait.20object.html#230739109">(Mar 17 2021 at 18:03)</a>:</h4>
<p>Of course. Forgot that <code>Mutex</code> doesn't actually allocate any memory, so <code>Mutex&lt;dyn Subscriber&gt;</code> itself is a DST, which means that the vtable pointer is stored as part of the fat pointer inside <code>Arc</code>.</p>



<a name="230739386"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Cast%20to%20trait%20object/near/230739386" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Cast.20to.20trait.20object.html#230739386">(Mar 17 2021 at 18:05)</a>:</h4>
<p>Er, <code>Mutex</code> does actually allocate some memory for the OS mutex to make it possible to move the <code>Mutex</code> without violating the api constraints of many OS mutexes. It just doesn't allocate memory for the wrapped value.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>